package hikari.objects3d.light;

import com.gooddies.wiring.Wiring;
import hikari.managers.EngineProperties;
import hikari.managers.Manager3D;
import hikari.math.CircleDrawer;
import hikari.utils.Utils;
import org.swig.jirr.E_LIGHT_TYPE;
import org.swig.jirr.ISceneManager;
import org.swig.jirr.ITexture;
import org.swig.jirr.IVideoDriver;
import org.swig.jirr.vector3df;

/**
 * @author Dmitry
 */
public class ConeLight3dObject extends Light3dObject {

    private static ITexture billboardTexture;

    public ConeLight3dObject(ISceneManager sceneManager) {
        super(sceneManager, E_LIGHT_TYPE.ELT_CONE, "ConeLight");
    }

    @Override
    public void drawGizmos() {
        IVideoDriver driver = Wiring.getComponent(Manager3D.class).getVideoDriver();
        driver.resetTransformMatrix();
        driver.setMaterial(Wiring.getComponent(EngineProperties.class).getSMaterialProperty("lightGizmoMaterialFront"));
        float length=getLightNode().getRadius();
        vector3df position = getRotation().rotationToDirection(new vector3df(0, 0, length));
        float angle=getLightNode().getLightData().getOuterCone()/2.0f;
        float beamRadius=(float)(length*Math.tan(Math.toRadians(angle)));
        
        position=position.addOperator(getMeshSceneNode().getAbsolutePosition());
        float rot=getRotation().getZ();
        CircleDrawer circle = new CircleDrawer(20, position, getRotation().rotationToDirection(), beamRadius, rot);
        circle.drawCircle();
 
        driver.draw3DLine(getMeshSceneNode().getAbsolutePosition(), circle.getPoint(0));
        driver.draw3DLine(getMeshSceneNode().getAbsolutePosition(), circle.getPoint(90));
        driver.draw3DLine(getMeshSceneNode().getAbsolutePosition(), circle.getPoint(180));
        driver.draw3DLine(getMeshSceneNode().getAbsolutePosition(), circle.getPoint(270));
    }

    @Override
    public String objectType() {
        return "coneLight";
    }

    @Override
    protected ITexture getBillboardTexture() {
        if (billboardTexture == null) {
            billboardTexture = Wiring.getComponent(Utils.class).loadTextureFromResource("icons/omniLight.png");
        }
        return billboardTexture;
    }
}
